﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using com.tiyma.common;
using com.tiyma.model;
using System.Text.RegularExpressions;
namespace com.tiyma.components
{
    public class SEOManager
    {
        /// <summary>
        /// 百度搜索小说信息
        /// </summary>
        /// <param name="i_key_word"></param>
        /// <returns></returns>
        public List<BookInfo> BaiDuSearch(string i_key_word)
        {
            /// <summary>
            /// 百度首页编码
            /// </summary>
            Encoding t_encoding = Encoding.UTF8;

            string t_search_url = string.Format("http://www.baidu.com/s?wd={0}", HttpUtility.UrlEncode(string.Format("小说 {0} 最新章节", i_key_word)));
            
            string t_reg="<h3\\s*?class=[\'\"]?t[\'\"]?>\\s*?<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</h3>\\s*?<font[^<>]*?size=[\'\"]*?-1[^<>]*?>(.*?)<br>\\s*?<span[^<>]*?>[^<>]*?(\\d{4}-[\\d{2}|\\d{1}]+-[\\d{2}|\\d{1}][^<>]*?)</span>";

            List<BookInfo> t_list = null;

            DateTime t_update_time = DateTime.Now;

            string t_book_name = string.Empty;

            try
            {
                string t_html =HttpHelper.GetHttpRequestStringByNUll_Post(t_search_url);

                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma == null || t_ma.Count <= 0)
                        return null;

                    t_list = new List<BookInfo>();

                    foreach (Match t_mc in t_ma)
                    {

                        if (t_mc.Groups.Count >=4)
                        {
                            if (t_mc.Groups[1].Value.ToString().Trim().ToLower().IndexOf("baidu") > -1 || t_mc.Groups[1].Value.ToString().Trim().ToLower().IndexOf("bbs")>-1)
                                continue;

                            BookInfo t_book = new BookInfo();

                            t_book_name = t_mc.Groups[2].Value.ToString();

                            t_book_name = Regex.Replace(t_book_name, "<em>小说</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book_name = Regex.Replace(t_book_name, "<em>最新章节</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book_name = Regex.Replace(t_book_name, "<em>最新</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book_name = Regex.Replace(t_book_name, "<em>章节</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                            t_book.BookName = t_book_name;
                            t_book.Description = Regex.Replace(t_mc.Groups[3].Value.ToString(), "<em>小说</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book.Description = Regex.Replace(t_book.Description, "<em>最新章节</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book.Description = Regex.Replace(t_book.Description, "<em>最新</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_book.Description = Regex.Replace(t_book.Description, "<em>章节</em>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            if (!DateTime.TryParse(t_mc.Groups[4].Value.ToString().Trim(), out t_update_time))
                                t_update_time = DateTime.Now;

                            t_book.LastUpdateTime = t_update_time;

                            t_book.BookChapterListUrl = t_mc.Groups[1].Value.ToString().Trim();

                            t_list.Add(t_book);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("从百度搜索小说失败" + ex.ToString());
            }
            return t_list;
        }
    }
}
